package device

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/device"
	deviceReq "github.com/flipped-aurora/gin-vue-admin/server/model/device/request"
)

type InformationService struct{}

// CreateInformation 创建information表记录
// Author [yourname](https://github.com/yourname)
func (informationService *InformationService) CreateInformation(ctx context.Context, information *device.Information) (err error) {
	err = global.GVA_DB.Create(information).Error
	return err
}

// DeleteInformation 删除information表记录
// Author [yourname](https://github.com/yourname)
func (informationService *InformationService) DeleteInformation(ctx context.Context, ID string) (err error) {
	err = global.GVA_DB.Delete(&device.Information{}, "id = ?", ID).Error
	return err
}

// DeleteInformationByIds 批量删除information表记录
// Author [yourname](https://github.com/yourname)
func (informationService *InformationService) DeleteInformationByIds(ctx context.Context, IDs []string) (err error) {
	err = global.GVA_DB.Delete(&[]device.Information{}, "id in ?", IDs).Error
	return err
}

// UpdateInformation 更新information表记录
// Author [yourname](https://github.com/yourname)
func (informationService *InformationService) UpdateInformation(ctx context.Context, information device.Information) (err error) {
	err = global.GVA_DB.Model(&device.Information{}).Where("id = ?", information.ID).Updates(&information).Error
	return err
}

// GetInformation 根据ID获取information表记录
// Author [yourname](https://github.com/yourname)
func (informationService *InformationService) GetInformation(ctx context.Context, ID string) (information device.Information, err error) {
	err = global.GVA_DB.Where("id = ?", ID).First(&information).Error
	return
}

// GetInformationInfoList 分页获取information表记录
// Author [yourname](https://github.com/yourname)
func (informationService *InformationService) GetInformationInfoList(ctx context.Context, info deviceReq.InformationSearch) (list []device.Information, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&device.Information{})
	var informations []device.Information
	// 如果有条件搜索 下方会自动创建搜索语句
	if len(info.CreatedAtRange) == 2 {
		db = db.Where("created_at BETWEEN ? AND ?", info.CreatedAtRange[0], info.CreatedAtRange[1])
	}

	if info.EquipmentModel != nil && *info.EquipmentModel != "" {
		db = db.Where("equipment_model = ?", *info.EquipmentModel)
	}
	if info.DeviceName != nil && *info.DeviceName != "" {
		db = db.Where("device_name LIKE ?", "%"+*info.DeviceName+"%")
	}
	if info.DataType != nil && *info.DataType != "" {
		db = db.Where("data_type = ?", *info.DataType)
	}
	if info.DataFormat != nil && *info.DataFormat != "" {
		db = db.Where("data_format = ?", *info.DataFormat)
	}
	if info.DataTitle != nil && *info.DataTitle != "" {
		db = db.Where("data_title LIKE ?", "%"+*info.DataTitle+"%")
	}
	if info.FileSize != nil {
		db = db.Where("file_size = ?", *info.FileSize)
	}
	if info.UpdateDate != nil {
		db = db.Where("update_date = ?", *info.UpdateDate)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&informations).Error
	return informations, total, err
}
func (informationService *InformationService) GetInformationPublic(ctx context.Context) {
	// 此方法为获取数据源定义的数据
	// 请自行实现
}
